package cn.demarcia.coco.ui.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.demarcia.coco.R;
import cn.demarcia.coco.ui.adapter.HistoryAdapter;
import cn.demarcia.coco.uihelper.StringUtils;
import cn.demarcia.coco.uihelper.ToastUtils;
import info.jimao.newsdk.JuHeApi;
import info.jimao.newsdk.callbacks.JuHeListDataCallback;
import info.jimao.newsdk.callbacks.SingleDataCallback;
import info.jimao.newsdk.models.AlmanacModel;
import info.jimao.newsdk.models.HistoryModel;
import info.jimao.newsdk.results.JuHeListDataResult;
import info.jimao.newsdk.results.SingleDataResult;
import okhttp3.Call;

/**
 * Created by lzy
 * on 2016/8/1.
 */
public class CalendarActivity extends BaseActivity implements OnDateSelectedListener {
    @Bind(R.id.mvCalendar)
    MaterialCalendarView mvCalendar;
    @Bind(R.id.tvNongLi)
    TextView tvNongLi;
    @Bind(R.id.tvJi)
    TextView tvJi;
    @Bind(R.id.tvBaiJi)
    TextView tvBaiJi;
    @Bind(R.id.tvChSha)
    TextView tvChongSha;
    @Bind(R.id.tvJiShYiQu)
    TextView tvJiShYiQu;
    @Bind(R.id.tvXShYJi)
    TextView tvXShYJi;
    @Bind(R.id.tvWuXing)
    TextView tvWuXing;
    @Bind(R.id.tvYi)
    TextView tvYi;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.tvYangLi)
    TextView tvYangLi;
    @Bind(R.id.ivHistoryToggleArrow)
    ImageView ivToggle;
    @Bind(R.id.recyclerViewHistory)
    RecyclerView recyclerViewHistory;
    @Bind(R.id.llAlmanacList)
    LinearLayout llAlmanacList;
    @Bind(R.id.ivAlmanacToggleArrow)
    ImageView ivAlToggle;
    private Date dateSelected;
    private CalendarDay selectedDay;
    private AlmanacModel almanacModel;
    private Boolean isHistoryExpanded;
    private Boolean isAlmanacExpanded;
    private LinearLayoutManager layoutManager;
    private RecyclerView.Adapter adapter;
    private List<Object> items = new ArrayList<>();
    private int month = 0;
    private int day = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calendar);
        ButterKnife.bind(this);
        toolbar.setTitle(R.string.calendar);  //noinspection deprecation
        toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);

        setSupportActionBar(toolbar);

        isHistoryExpanded = false;
        isAlmanacExpanded = false;
        Calendar c = Calendar.getInstance();
        mvCalendar.setSelectedDate(c);
        mvCalendar.setOnDateChangedListener(this);
        selectedDay = mvCalendar.getSelectedDate();
        dateSelected = new Date();
        dateSelected.setDate(selectedDay.getDay());
        dateSelected.setMonth(selectedDay.getMonth());
        dateSelected.setYear(selectedDay.getYear() - 1900);

        month = selectedDay.getMonth() + 1;
        day = selectedDay.getDay();

        adapter = new HistoryAdapter(this, items);
        layoutManager = new GridLayoutManager(this, 2);
        recyclerViewHistory.setLayoutManager(layoutManager);
        recyclerViewHistory.setAdapter(adapter);

        loadAlmanacData(dateSelected);

        loadHistoryData(month, day);
    }

    private void loadHistoryData(int month, int day) {
        JuHeApi api = new JuHeApi(this);
        api.getHistory(month, day, new JuHeListDataCallback<HistoryModel>(HistoryModel.class) {
            @Override
            public void onError(Call call, Exception e) {
                super.onError(call, e);
                ToastUtils.show(CalendarActivity.this, e.getMessage());
            }

            @Override
            public void onResponse(JuHeListDataResult<HistoryModel> response) {
                super.onResponse(response);

                items.clear();
                items.addAll(response.getDatas());
                adapter.notifyDataSetChanged();
            }
        });
    }

    private void loadAlmanacData(Date date) {

        JuHeApi almanacApi = new JuHeApi(this);
        almanacApi.getAlmanac(StringUtils.timeFormat(date, "yyyy-MM-dd"), new SingleDataCallback<AlmanacModel>(AlmanacModel.class) {
            @Override
            public void onError(Call call, Exception e) {
                super.onError(call, e);
                ToastUtils.show(CalendarActivity.this, e.getMessage());
            }

            @Override
            public void onResponse(SingleDataResult<AlmanacModel> response) {
                super.onResponse(response);
                almanacModel = response.getResult();
                refreshAlmanacView(almanacModel);
            }
        });


    }

    private void refreshAlmanacView(AlmanacModel almanac) {
        tvNongLi.setText(almanac.yinli);
        tvJi.setText(almanac.ji);
        tvBaiJi.setText(almanac.baiji);
        tvChongSha.setText(almanac.chongsha);
        tvXShYJi.setText(almanac.xiongshen);
        tvJiShYiQu.setText(almanac.jishen);
        tvWuXing.setText(almanac.wuxing);
        tvYi.setText(almanac.yi);
        tvYangLi.setText(almanac.yangli);
    }

    @Override
    public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
        dateSelected.setDate(date.getDay());
        dateSelected.setMonth(date.getMonth());
        dateSelected.setYear(date.getYear() - 1900);

        month = date.getMonth() + 1;
        day = date.getDay();

        loadHistoryData(month, day);

        loadAlmanacData(dateSelected);
    }

    @OnClick(R.id.rlHistoryHolder)
    public void toggleHistoryList() {
        if (isHistoryExpanded) {
            isHistoryExpanded = false;
            ivToggle.setImageResource(R.drawable.ic_expand_more_black_24dp);
            ivToggle.setEnabled(true);
            recyclerViewHistory.setVisibility(View.GONE);
        } else {
            isHistoryExpanded = true;
            ivToggle.setImageResource(R.drawable.ic_expand_less_black_24dp);
            ivToggle.setEnabled(true);
            recyclerViewHistory.setVisibility(View.VISIBLE);
        }
    }

    @OnClick(R.id.rlAlmanacHolder)
    public void toggleAlmanacList() {
        if (isAlmanacExpanded) {
            isAlmanacExpanded = false;
            ivAlToggle.setImageResource(R.drawable.ic_expand_more_black_24dp);
            ivAlToggle.setEnabled(true);
            llAlmanacList.setVisibility(View.GONE);
        } else {
            isAlmanacExpanded = true;
            ivAlToggle.setImageResource(R.drawable.ic_expand_less_black_24dp);
            ivAlToggle.setEnabled(true);
            llAlmanacList.setVisibility(View.VISIBLE);
        }
    }
}
